﻿#region Library
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using System.Collections;
using Crystal_Report_WithoutDB;
using CRM_SMS_MARKETING.PL.Customer_Care;
using System.Threading;
#endregion

namespace CRM_SMS_MARKETING.PL.Message
{
    public partial class frmComposeMessage : DevExpress.XtraEditors.XtraForm
    {
        #region Attributes
        frmContact frmContact;
        public static List<GSM.BusinessLayer.Customer> ListCustomer;
        CustomerSql customersql;
        Message_MarketSql message_MarketSql;
        Customer_MessageSql customer_MessageSql;
        #endregion

        #region Method
        public frmComposeMessage(List<SmsControl.SmsControl> List_Modem)
        {
            InitializeComponent();
            list_Modem = List_Modem;
            ListCustomer = new List<GSM.BusinessLayer.Customer>();
            customersql= new CustomerSql();
            message_MarketSql= new Message_MarketSql();
            customer_MessageSql = new Customer_MessageSql();
        }
        
        private void btnContact_Click(object sender, EventArgs e)
        {
            if (frmContact == null)
            {
                frmContact = new frmContact();
                frmContact.ShowDialog();
                gridControlCustomer.DataSource = ListCustomer;
                //for (int i = 0; i < ListCustomer.Count; i++)
                //{
                //    gridViewCustomer.AddNewRow();
                //    gridViewCustomer.SetRowCellValue(i, colID, ListCustomer[i].CustomerID);
                //    gridViewCustomer.SetRowCellValue(i, colFirstName, ListCustomer[i].FirstName);
                //    gridViewCustomer.SetRowCellValue(i, colLastName, ListCustomer[i].LastName);
                //    gridViewCustomer.SetRowCellValue(i, colPhone, ListCustomer[i].CellPhone);
                //}
         
            }
            else
            {
                frmContact.ShowDialog();
                gridControlCustomer.DataSource = ListCustomer;
            }
        }
        private void btnNew_Click(object sender, EventArgs e)
        {

            gridViewCustomer.AddNewRow();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            MessageBox.Show(gridViewCustomer.GetRowCellValue(3, colID).ToString());
        }
        
        #endregion
        private void frmComposeMessage_Load(object sender, EventArgs e)
        {
            //DataTable t1 = new DataTable();
            //t1.Columns.Add(new DataColumn("CustomerID", typeof(int)));
            //t1.Columns.Add(new DataColumn("FistName", typeof(string)));
            //t1.Columns.Add(new DataColumn("LastName", typeof(string)));
            //t1.Columns.Add(new DataColumn("CellPhone", typeof(double)));

            //gridControlCustomer.DataSource = t1;

            bll_SendMessage = new BLL_SendMessage(list_Modem[0]);
            thread = new Thread(bll_SendMessage.send);
            thread.Start();
            
            
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                Message_Market message = new Message_Market();
                //Check List Rong
                if (gridViewCustomer.RowCount == 0)
                {
                    MessageBox.Show("List of received contact is empty! Please choose contacts from data by press Contact Button or Enter contacts to list");
                }
                // Quyet toan bo gridview va luu xuong du lieu bang Message_Market va Customer_Message (dang outbox)
                else
                {
                    //Thuc hien nhung customer trong du lieu truoc


                    //Luu Message
                    message.Content = txtContent.Text;
                    message_MarketSql.Insert(message);
                    //Luu Customer Message

                    int messageID = (int)message_MarketSql.GetLastReCordID();
                    int CustomerCount = gridViewCustomer.RowCount;
                    for (int i = 0; i < CustomerCount; i++)
                    {
                        Customer_Message CM = new Customer_Message();
                        if (gridViewCustomer.GetRowCellValue(i, colID).ToString() != "")
                        {
                            CM.CustomerID = (int)gridViewCustomer.GetRowCellValue(i, colID);
                            CM.MessageMarketID = messageID;
                            CM.Type = 2;
                            CM.Date = DateTime.Now;
                            CM.Phone = ListCustomer[i].CellPhone;
                            CM.Content = txtContent.Text;
                            CM.Deleted = false;
                            customer_MessageSql.Insert(CM);
                        }
                        else
                        {
                            CM.CustomerID = 1;
                            CM.MessageMarketID = messageID;
                            CM.Type = 2;
                            CM.Content = txtContent.Text;
                            CM.Deleted = false;
                            CM.Date = DateTime.Now;
                            CM.Phone = gridViewCustomer.GetRowCellValue(i, colPhone).ToString();
                            customer_MessageSql.Insert(CM);
                        }
                    }
                    //Roi code o day di! Lay du lieu len ma gui!
                    if (thread.ThreadState != ThreadState.Running)
                    {
                        thread = new Thread(bll_SendMessage.send);
                        thread.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        List<SmsControl.SmsControl> list_Modem;
        BLL_SendMessage bll_SendMessage;
        Thread thread;
        
    }
}